#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include"system.h"
#include "y.tab.h"
// int INTEGER=50,KEYWORD=100;

struct ScopeTableNode *CreateNewScopeTableNode()
{
  int i=0;
  struct ScopeTableNode *t;
  t =(struct ScopeTableNode *)malloc(sizeof(struct ScopeTableNode));
  t->scope=NULL;
  t->next=NULL;
  for(i=0;i<HashSize;i++)
    t->table[i] = NULL;	
  return t;
}

struct ScopeEntry* Scopelookup(char *name)
{
  struct ScopeEntry *r,*s;
  r=NULL;
  struct ScopeTableNode *temp;
  temp=Global;
  int i=0,k=name[0]%HashSize;  
  while(temp->next!=NULL)
  {    
    s=temp->table[k];   
    if(s!=NULL)
    {
      if(strcmp(s->id,name)==0)
      {
	r=s;
      }    
      else
      {	
	while(s->next!=NULL)
	{
	  s=s->next;
	  if(strcmp(s->id,name)==0)
	  {
	    r=s;
	    break;
	  }
	}
      }      
    }
    temp=temp->next;
  } 
  s=temp->table[k];  
  if(s!=NULL)
  {
    if(strcmp(s->id,name)==0)
    {
      r=s;
    }    
    else
    {      
      while(s->next!=NULL)
      {
	s=s->next;
	if(strcmp(s->id,name)==0)
	{
	  r=s;
	  break;
	}
      }
    }  
  }  
  return r;
}
struct ScopeEntry *InsertScopeTable(char *name,char* currentScope,char* typ)
{
  if(Tail->scope == NULL)
  {
    printf("NewScope : %s.\n",currentScope);
    Tail->scope=(char *)malloc(sizeof(char)*strlen(currentScope)+1);
    strcpy(Tail->scope,currentScope);
  }
  else if(strcmp(Tail->scope,currentScope)!=0)
  {  
    printf("NewScope : %s.\n",currentScope);
    struct ScopeTableNode *temp;
    temp=CreateNewScopeTableNode();
    temp->scope=(char *)malloc(sizeof(char)*strlen(currentScope)+1);
    strcpy(temp->scope,currentScope);	
    Tail->next=temp;
    Tail=temp;
  }  
  
  struct ScopeEntry *p;
  p= (struct ScopeEntry *)malloc(sizeof(struct ScopeEntry));
  p->id=(char *)malloc(sizeof(char)*strlen(name)+1);
  p->scope=(char *)malloc(sizeof(char)*strlen(currentScope)+1);
  p->type=(char *)malloc(sizeof(char)*strlen(typ)+1);
  strcpy(p->id,name);
  strcpy(p->scope,currentScope);
  strcpy(p->type,typ);
  p->next=Tail->table[name[0]%HashSize];
  Tail->table[name[0]%HashSize]=p;  
  return p;
}

void InitializeScopeTable()
{
  Global=CreateNewScopeTableNode();
  
  Tail=Global;
  InsertScopeTable("ACA","GLOBAL","INT");
  InsertScopeTable("BCA","GLOBAL","INT1");
  InsertScopeTable("ACAa","GLOBALa","INTa");
  InsertScopeTable("ACa","GLOBALa","INTA");
}

void PopScope()
{
  struct ScopeTableNode* temp;
  temp=Global;
  while(temp->next->next!=NULL)
  {
      temp=temp->next;
  }
  temp->next=NULL;
  free(Tail);
  Tail=temp;
}
/*int main()
{
  InitializeScopeTable();
  
  struct ScopeEntry *p;
  p=Scopelookup("ACAa");
  printf("\n%s--%s--%s\n",p->id,p->scope,p->type);
  PopScope();
  p=Scopelookup("ACAa");
  if(p==NULL)
    printf("Variable not found\n");
  else printf("\n%s--%s--%s\n",p->id,p->scope,p->type);
  
}
/*int main()
{
  printf("You are in.\n");
  InitializeSymTable();  
  printf("Global Node bana.\n");
  InsertSymTable("ABC1",1,1111);//INTEGER);
  printf("ABC1hmm inserted.\n");
  InsertSymTable("ABC2",1,2222);//FLOAT,(void *)"10.910");
  printf("ABC2 inserted.\n");
  InsertSymTable("BBC3",2,3333);//"POINTER TO CHAR",(void *)"asdad");
  printf("BBC3 inserted.\n");
  struct SymEntry *p;
  p=lookup("BBC3");  
  //p=global->next->table[2];
  printf("Searched for BBC3.Tail ka scope : %d\n",tail->scope);
  if(p==NULL)
    printf("Variable BBC3 not found.\n");
  else printf("Variable found.Name : %s, Type : %d , Scope : %d, Value : %d\n",p->id,p->type,p->scope,p->keywordval);
}*/